//
//  SKOptionalActionView.swift
//  ST_ST_SKPhotoBrowser
//
//  Created by keishi_suzuki on 2017/12/19.
//  Copyright © 2017年 suzuki_keishi. All rights reserved.
//

import UIKit

class ST_SKActionView: UIView {
    internal weak var browser: ST_ST_SKPhotoBrowser?
    internal var close_button: ST_SKCloseButton!
    internal var delete_button: ST_SKDeleteButton!
    
    // Action
    fileprivate var cancelTitle = "Cancel"
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(frame: CGRect, browser: ST_ST_SKPhotoBrowser) {
        self.init(frame: frame)
        self.browser = browser

        configureCloseButton()
        configureDeleteButton()
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let view = super.hitTest(point, with: event) {
            if close_button.frame.contains(point) || delete_button.frame.contains(point) {
                return view
            }
            return nil
        }
        return nil
    }
    
    func update_frame(frame: CGRect) {
        self.frame = frame
        setNeedsDisplay()
    }

    func updateCloseButton(image: UIImage, size: CGSize? = nil) {
        configureCloseButton(image: image, size: size)
    }
    
    func updateDeleteButton(image: UIImage, size: CGSize? = nil) {
        configureDeleteButton(image: image, size: size)
    }
    
    func animate(hidden: Bool) {
        let closeFrame: CGRect = hidden ? close_button.hideFrame : close_button.showFrame
        let deleteFrame: CGRect = hidden ? delete_button.hideFrame : delete_button.showFrame
        UIView.animate(withDuration: 0.35,
                       animations: { () -> Void in
                        let alpha: CGFloat = hidden ? 0.0 : 1.0

                        if ST_ST_ST_SKPhotoBrowserOptions.displayCloseButton {
                            self.close_button.alpha = alpha
                            self.close_button.frame = closeFrame
                        }
                        if ST_ST_ST_SKPhotoBrowserOptions.displayDeleteButton {
                            self.delete_button.alpha = alpha
                            self.delete_button.frame = deleteFrame
                        }
        }, completion: nil)
    }
    
    @objc func closeButtonPressed(_ sender: UIButton) {
        browser?.determineAndClose()
    }
    
    @objc func deleteButtonPressed(_ sender: UIButton) {
        guard let browser = self.browser else { return }
        
        browser.delegate?.removePhoto?(browser, index: browser.currentPageIndex) { [weak self] in
            self?.browser?.deleteImage()
        }
    }
}

extension ST_SKActionView {
    func configureCloseButton(image: UIImage? = nil, size: CGSize? = nil) {
        if close_button == nil {
            close_button = ST_SKCloseButton(frame: .zero)
            close_button.addTarget(self, action: #selector(closeButtonPressed(_:)), for: .touchUpInside)
            close_button.isHidden = !ST_ST_ST_SKPhotoBrowserOptions.displayCloseButton
            addSubview(close_button)
        }

        if let size = size {
            close_button.set_frame_size(size)
        }
        
        if let image = image {
            close_button.setImage(image, for: .normal)
        }
    }
    
    func configureDeleteButton(image: UIImage? = nil, size: CGSize? = nil) {
        if delete_button == nil {
            delete_button = ST_SKDeleteButton(frame: .zero)
            delete_button.addTarget(self, action: #selector(deleteButtonPressed(_:)), for: .touchUpInside)
            delete_button.isHidden = !ST_ST_ST_SKPhotoBrowserOptions.displayDeleteButton
            addSubview(delete_button)
        }
        
        if let size = size {
            delete_button.set_frame_size(size)
        }
        
        if let image = image {
            delete_button.setImage(image, for: .normal)
        }
    }
}
